﻿using System;
using Csla.Xaml;
using Accounting.Library;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;



namespace csla4Skeleton.UI.SL.NghiepVu.THUE.THUE_BangKeBanRa
{
    public class THUE_BangKeBanRa_ViewModel : ViewModel<Accounting.Library.THUE_BangKeBanRa>
    {
        #region Event Declare
        public delegate void ChildList_AddedNew_EventHandler(object sender, ChildListAddedNewArgs e);
       
        public delegate void SoChiTietIdChanged_EventHandler(object sender, SoChiTietIdChangedEventArgs e);
        
        public event ChildList_AddedNew_EventHandler ChildList_AddedNew;

       
        public event EventHandler CountChanged;
        public event EventHandler LoadCompleted;
        public event EventHandler Saved;

        public event EventHandler Canceled;
        public event SoChiTietIdChanged_EventHandler SoChiTietIdChanged;
       
        #endregion

        #region Refresh Model
        public THUE_BangKeBanRa_ViewModel()
        {
            BeginRefresh("NewTHUE_BangKeBanRa");
            
        }

        public THUE_BangKeBanRa_ViewModel(string mode)
        {
            if (mode.Equals(BusinessConfiguration.Configuration.ChildWindow))
            {
                this.BeginRefresh("NewTHUE_BangKeBanRa");
            }
           
        }
        public THUE_BangKeBanRa_ViewModel(int Khoa)
        {
            BeginRefresh("GetTHUE_BangKeBanRa", Khoa);
           
        }

        public void NewTHUE_BangKeBanRaViewModel()
        {
            BeginRefresh("NewTHUE_BangKeBanRa");
            
        }

        public void GetTHUE_BangKeBanRaViewModel(int Khoa)
        {
            BeginRefresh("GetTHUE_BangKeBanRa", Khoa);
        }

       
        public void SaveAndNew()
        {
            BeginSave();
            NewTHUE_BangKeBanRaViewModel();
        }
        protected override void OnRefreshed()
        {
            base.OnRefreshed();
            if (Model != null)
            {
                Model.THUE_BangKeBanRa_ChiTiets.AddedNew += new EventHandler<Csla.Core.AddedNewEventArgs<THUE_BangKeBanRa_ChiTietChild>>(THUE_BangKeBanRa_ChiTiets_AddedNew);              
                Model.SoChiTietIdChanged += new EventHandler<SoChiTietIdChangedEventArgs>(OnSoChiTietIdChanged);               
                
                Model.BusyEvent += new EventHandler<IsBusyEventArgs>(DoChangingBusy);
                Model.FinishAddNew += new EventHandler(OnCountChanged);
                if (LoadCompleted != null)
                {
                    LoadCompleted(this, new EventArgs());
                }
            }
        }
        #endregion
        
        #region Add THUE_BangKeBanRa Details Item
        public void AddTHUE_BangKeBanRa_ChiTietItem()
        {
            Model.THUE_BangKeBanRa_ChiTiets.AddNewItem(Model.Khoa);
        }


        void THUE_BangKeBanRa_ChiTiets_AddedNew(object sender, Csla.Core.AddedNewEventArgs<THUE_BangKeBanRa_ChiTietChild> e)
        {
            OnChildList_AddedNew(sender, new ChildListAddedNewArgs("THUE_BangKeBanRa_ChiTiets"));
        }      

        protected virtual void OnChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (ChildList_AddedNew != null)
            {
                //Invokes the delegates.
                ChildList_AddedNew(sender, e);
            }
        }

       
        public void OnSoChiTietIdChanged(object sender, SoChiTietIdChangedEventArgs e)
        {
            if (SoChiTietIdChanged != null)
            {
                SoChiTietIdChanged(sender, e);
            }

        }
        public void OnCountChanged(object sender, EventArgs e)
        {
            if (CountChanged != null)
            {
                CountChanged(sender, e);
            }

        }
        public void DoChangingBusy(object sender, IsBusyEventArgs e)
        {
            IsBusy = e.Busy;
        }
        public void DoChangingBusy(bool busy)
        {
            IsBusy = busy;
        }
        

        protected override void OnSaved()
        {
            base.OnSaved();
            if (Saved != null)
            {
                Saved(this, EventArgs.Empty);
            }
           
        }

        protected override void DoCancel()
        {
            base.DoCancel();
            if (Canceled != null)
            {
                Canceled(this, EventArgs.Empty);
            }
            
        }

      
        #endregion

        #region Delete THUE_BangKeBanRa Details Item

        public void DeleteTHUE_BangKeBanRa_ChiTietItem(int Index)
        {
            Model.THUE_BangKeBanRa_ChiTiets.DeleteItem(Index);
        }

        

        #endregion

        #region Nghiep Vu So
        internal void GhiSo()
        {
            this.Model.SaveMode = BusinessConfiguration.Configuration.GhiSoSaveMode;
            BeginSave();
        }

        internal void BoGhi()
        {
            int loaiChungTuId = BusinessConfiguration.Configuration.LoaiChungTuId_THUE_BangKeBanRa;
            int chungTuGocId = Model.Khoa;

            this.IsBusy = true;
            Accounting.Library.GhiSo.BoGhiService.BoGhi(loaiChungTuId, chungTuGocId, (o, ex) =>
            {
                if (ex.Error != null)
                    this.Model.ErrorMessage = ex.Error.Message;
                else
                    this.Model.ErrorMessage = ((Accounting.Library.GhiSo.BoGhiService)ex.Object).ErrorMessage;
                BeginRefresh("GetTHUE_BangKeBanRa", Model.Khoa);
            });
            this.IsBusy = false;
        }
#endregion
    }
}
